﻿using Microsoft.AspNetCore.Mvc;
using PublicData;
using PublicModelsAndEntities.Models.MainsortRecord;
using XinjeMesProgram.Entities;

namespace MesAPI.Controllers.Maintenance
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaintenanceDataController : ControllerBase
    {
        private readonly ResponseModel response = new();

        [Route("/api/v1/maintenancedata")]
        [HttpGet]
        public ResponseModel MainQuery(string deliveryNumberIn)
        {
            using (var context = new FactoryMesContext())
            {
                var query = from sr in context.MainsortRecord // 主表
                            join cost in context.MaintenanceCost // 左连接维修成本表
                              on sr.ID equals cost.ID into costGroup
                            from cost in costGroup.DefaultIfEmpty()
                            join rp in context.RepairProduct // 左连接维修产品表
                              on sr.ID equals rp.ProductId into rpGroup
                            from rp in rpGroup.DefaultIfEmpty()
                            join d in context.Deliver // 左连接交付表
                              on sr.ID equals d.ProductId into dGroup
                            from d in dGroup.DefaultIfEmpty()
                            where sr.Delivery_NumberIn == deliveryNumberIn
                            select new MaintenanceQueryModel
                            {
                                WarehousingDate = sr.WarehousingDate.Substring(0, 10),  // 收货时间
                                One_dimensional_code = sr.One_Dimensional_Code,         // 产品序列号
                                Product_model = sr.Product_Model,                       // 产品型号
                                State = sr.State == "7" ? "已包装" : sr.State == "8" ? "已发货" : "维修中", // 状态
                                Delivery_nameOut = d.NameOut,                           // 发货快递名称
                                Delivery_numberOut = d.NumberOut,                       // 发货快递单号
                            };
                List<MaintenanceQueryModel> result = query.ToList();
                int a = result.Count;
                this.response.SetData(result, result.Count);
            }

            return this.response;
        }
    }
}
